package com.goods.controller.business;

import com.goods.business.service.ProductService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @GetMapping("/findProductList")
    public ResponseBean findProductList(Integer pageNum,Integer pageSize,String name,Long[] categorys,String supplier,int status){
        Map map = productService.findProductList(pageNum,pageSize,name,categorys,supplier,status);
        return ResponseBean.success(map);
    }

    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductVO productVO){
        productService.add(productVO);
        return ResponseBean.success();
    }

    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductVO productVO = productService.edit(id);
        return ResponseBean.success(productVO);
    }

    @PutMapping("/update/{id}")
    public ResponseBean updateById(@PathVariable Long id,@RequestBody ProductVO productVO){
        int i = productService.updateById(productVO);
        if (i != 0){
            return ResponseBean.success();
        }
        return ResponseBean.error("修改失败");
    }

    //移入回收站
    @PutMapping("/remove/{id}")
    public ResponseBean removeById(@PathVariable Long id){
        int i = productService.removeById(id);
        if (i != 0) {
            return ResponseBean.success();
        }else {
            return ResponseBean.error("移入回收站失败");
        }
    }

    //从回收站恢复成正常状态
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        int i = productService.back(id);
        if (i != 0) {
            return ResponseBean.success();
        }else {
            return ResponseBean.error("移入回收站失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        int i = productService.deleteById(id);
        if (i != 0) {
            return ResponseBean.success();
        }else {
            return ResponseBean.error("删除失败");
        }
    }

    //审核
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        int i = productService.publish(id);
        if (i != 0) {
            return ResponseBean.success();
        }else {
            return ResponseBean.error("审核失败");
        }
    }

    @GetMapping("/findProducts")
    public ResponseBean findProducts(Integer pageNum, Integer pageSize, Integer status,Long[] categorys,String name){
        Map map = productService.findProductList(pageNum,pageSize,name,categorys,null,status);
        return ResponseBean.success(map);
    }

    @GetMapping("/findProductStocks")
    public ResponseBean findProductStocks(Integer pageNum, Integer pageSize,Long[] categorys,String name){
        Map map = productService.findProductList(pageNum,pageSize,name,categorys,null,0);
        return ResponseBean.success(map);
    }

    @GetMapping("/findAllStocks")
    public ResponseBean findAllStocks(Integer pageNum, Integer pageSize){
        List<ProductStockVO> productStockVOList = productService.findAllStocks(pageNum,pageSize);
        return ResponseBean.success(productStockVOList);
    }
}
